package database_managment;

import java.sql.*;
import java.util.InputMismatchException;

/**
 * @file Database.java
 * @version 0.1
 * @author James Dibble 10009689
 */
public class Database {
    // <editor-fold defaultstate="collapsed" desc="Attributes">
    private String serverURL;
    private Connection sqlConnection;
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Constructors">
    public Database(String serverURL) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        this.serverURL = serverURL;
        Class.forName ("com.mysql.jdbc.Driver").newInstance();
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Methods">
    /**
     * Connect a database using the given parameters.
     * 
     * @param strUser
     * @param strPassword
     * @throws SQLException 
     */
    public void connect(String strUser, String strPassword) throws SQLException{
        sqlConnection = DriverManager.getConnection(serverURL, strUser, strPassword);
    }
    
    /**
     * Create an insert statement for a given set of columns.
     * 
     * @param strTable
     * @param strColumnsArr
     * @param strValuesArr 
     */
    public void executeInsertStatement(String strTable, 
                                       String[] strColumnsArr, 
                                       Object[] objValuesArr) throws InputMismatchException, SQLException, Exception{
        
        if(strColumnsArr.length != objValuesArr.length) 
            throw new Exception("Columns array is not the same length as values array.");
        
        String strTempCols = "";
        for(String strThis: strColumnsArr){
            strTempCols += strThis + ",";
        }
        strTempCols = strTempCols.substring(0, strTempCols.length() - 1);
        
        String strTempVals = "";
        for(Object objThis: objValuesArr){
            if(objThis instanceof String){
                strTempVals += "'" + objThis + "',";
            }
            else if(objThis instanceof Integer){
                strTempVals += objThis + ",";
            }
            else{
                throw new InputMismatchException("Incorrect object type passed.  Must be String or Integer not int.");
            }
        }
        strTempVals = strTempVals.substring(0, strTempVals.length() - 1);
        
        String strQuery = String.format("INSERT INTO %s(%s) VALUES (%s)",
                                        strTable,
                                        strTempCols,
                                        strTempVals);
        executeQuery(strQuery);
    }
    
    /**
     * Create an insert statement for all columns.
     * 
     * @param strTable
     * @param strValuesArr 
     */
    public void executeInsertStatement(String strTable,  
                                       Object[] objValuesArr) throws InputMismatchException, SQLException{       
        String strTempVals = "";
        for(Object objThis: objValuesArr){
            if(objThis instanceof String){
                strTempVals += "'" + objThis + "',";
            }
            else if(objThis instanceof Integer){
                strTempVals += objThis + ",";
            }
            else{
                throw new InputMismatchException("Incorrect object type passed.  Must be String or Integer not int.");
            }
        }
        strTempVals = strTempVals.substring(0, strTempVals.length() - 1);
        
        String strQuery = String.format("INSERT INTO %s VALUES (%s)",
                                        strTable,
                                        strTempVals);
        System.out.println(strQuery);
        executeQuery(strQuery);
    }
    
    private void executeQuery(String strQuery) throws SQLException{
        Statement thisStatement = sqlConnection.createStatement();
        thisStatement.executeUpdate(strQuery);
    }
    // </editor-fold>
}
